﻿using Teigha.Core;
using Teigha.TD;

namespace OdaOperation.Core.Creators
{
    public class CircleCreator : BaseCreator
    {
        /// <summary>
        /// 构造函数，接收一个数据库对象
        /// </summary>
        /// <param name="pDb"></param>
        public CircleCreator(OdDbDatabase pDb)
            : base(pDb)
        {
        }

        /// <summary>
        /// 构造函数，接收一个数据库对象和一个块表ID
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="blockTableId"></param>
        public CircleCreator(OdDbDatabase pDb, OdDbObjectId blockTableId)
            : base(pDb, blockTableId)
        {
        }

        /// <summary>
        /// 构造函数，接收一个数据库对象和一个图层名称
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="layerName"></param>
        public CircleCreator(OdDbDatabase pDb, string layerName)
            : base(pDb, layerName)
        {
        }

        /// <summary>
        /// 构造函数，接收一个数据库对象、一个块表ID和一个图层名称
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="blockTableId"></param>
        /// <param name="layerName"></param>
        public CircleCreator(OdDbDatabase pDb, OdDbObjectId blockTableId, string layerName)
            : base(pDb, blockTableId, layerName)
        {
        }

        /// <summary>
        /// 创建圆的方法，接收块表ID、图层名称、圆心和半径作为参数
        /// </summary>
        /// <param name="blockTableId"></param>
        /// <param name="layerName"></param>
        /// <param name="center"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        public OdDbCircle CreateCircle(OdDbObjectId blockTableId, string layerName, OdGePoint3d center, double radius)
        {
            // 打开块表记录对象，模式为写
            OdDbBlockTableRecord odDbBlockTableRecord = blockTableId.openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            // 创建一个新的圆对象
            OdDbCircle odDbCircle = OdDbCircle.createObject();
            // 设置圆的半径
            odDbCircle.setRadius(radius);
            // 设置圆的圆心
            odDbCircle.setCenter(center);
            // 设置圆的数据库默认值
            odDbCircle.setDatabaseDefaults(m_database);
            // 设置圆的图层
            odDbCircle.setLayer(layerName);
            // 将圆对象添加到块表记录中
            odDbBlockTableRecord.appendOdDbEntity(odDbCircle);
            return odDbCircle;
        }

        /// <summary>
        /// 创建圆的方法，接收图层名称、圆心和半径作为参数
        /// </summary>
        /// <param name="layerName"></param>
        /// <param name="center"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        public OdDbCircle CreateCircle(string layerName, OdGePoint3d center, double radius)
        {
            // 打开块表记录对象，模式为写
            OdDbBlockTableRecord odDbBlockTableRecord = m_blockTableId.openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            // 创建一个新的圆对象
            OdDbCircle odDbCircle = OdDbCircle.createObject();
            // 设置圆的半径
            odDbCircle.setRadius(radius);
            // 设置圆的圆心
            odDbCircle.setCenter(center);
            // 设置圆的数据库默认值
            odDbCircle.setDatabaseDefaults(m_database);
            // 设置圆的图层
            odDbCircle.setLayer(layerName);
            // 将圆对象添加到块表记录中
            odDbBlockTableRecord.appendOdDbEntity(odDbCircle);
            return odDbCircle;
        }

        /// <summary>
        /// 创建圆的方法，接收块表ID、圆心和半径作为参数
        /// </summary>
        /// <param name="blockTableId"></param>
        /// <param name="center"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        public OdDbCircle CreateCircle(OdDbObjectId blockTableId, OdGePoint3d center, double radius)
        {
            // 打开块表记录对象，模式为写
            OdDbBlockTableRecord odDbBlockTableRecord = blockTableId.openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            // 创建一个新的圆对象
            OdDbCircle odDbCircle = OdDbCircle.createObject();
            // 设置圆的半径
            odDbCircle.setRadius(radius);
            // 设置圆的圆心
            odDbCircle.setCenter(center);
            // 设置圆的数据库默认值
            odDbCircle.setDatabaseDefaults(m_database);
            // 设置圆的图层为默认图层名称
            odDbCircle.setLayer(m_layerName);
            // 将圆对象添加到块表记录中
            odDbBlockTableRecord.appendOdDbEntity(odDbCircle);
            return odDbCircle;
        }

        /// <summary>
        /// 创建圆的方法，接收圆心和半径作为参数
        /// </summary>
        /// <param name="center"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        public OdDbCircle CreateCircle(OdGePoint3d center, double radius)
        {
            // 打开块表记录对象（如果块表ID为空，则打开模型空间ID），模式为写
            OdDbBlockTableRecord odDbBlockTableRecord = (m_blockTableId ?? m_database.getModelSpaceId()).openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            // 创建一个新的圆对象
            OdDbCircle odDbCircle = OdDbCircle.createObject();
            // 设置圆的半径
            odDbCircle.setRadius(radius);
            // 设置圆的圆心
            odDbCircle.setCenter(center);
            // 设置圆的数据库默认值
            odDbCircle.setDatabaseDefaults(m_database);
            // 设置圆的图层为默认图层名称
            odDbCircle.setLayer(m_layerName);
            // 将圆对象添加到块表记录中
            odDbBlockTableRecord.appendOdDbEntity(odDbCircle);
            return odDbCircle;
        }
    }

}